//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.exception.BusinessError;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.engine.action.cmd.ActionCmd;
import com.dstz.bpm.api.engine.action.cmd.BaseActionCmd;
import com.dstz.bpm.api.engine.action.cmd.InstanceActionCmd;
import com.dstz.bpm.api.engine.action.cmd.TaskActionCmd;
import com.dstz.bpm.api.engine.action.handler.ActionHandler;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.exception.WorkFlowException;
import com.dstz.bpm.api.model.def.BpmDataModel;
import com.dstz.bpm.api.model.def.NodeInit;
import com.dstz.bpm.api.model.form.BpmForm;
import com.dstz.bpm.api.model.inst.IBpmInstance;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.model.task.IBpmTask;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.core.manager.BpmInstanceManager;
import com.dstz.bpm.core.manager.TaskIdentityLinkManager;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.bpm.engine.constant.TaskSkipType;
import com.dstz.bpm.engine.data.handle.BpmBusDataHandle;
import com.dstz.bpm.engine.model.DefaultBpmProcessDef;
import com.dstz.bpm.engine.util.HandlerUtil;
import com.dstz.bus.api.model.IBusinessData;
import com.dstz.bus.api.service.IBusinessDataService;
import com.dstz.org.api.model.IUser;
import com.dstz.sys.api.groovy.IGroovyScriptEngine;
import com.dstz.sys.util.ContextUtil;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

public abstract class AbsActionHandler<T extends BaseActionCmd> implements ActionHandler<T> {
   protected Logger LOG = LoggerFactory.getLogger(this.getClass());
   @Resource
   protected BpmProcessDefService a;
   @Resource
   protected BpmInstanceManager f;
   @Resource
   protected BpmBusDataHandle aC;
   @Resource
   protected TaskIdentityLinkManager p;
   @Resource
   protected IBusinessDataService aD;
   @Resource
   protected IGroovyScriptEngine aE;

   public AbsActionHandler() {
   }

   @Override
   @Transactional
   public void execute(T model) {
      this.c(model);
      this.l(model);
      BpmContext.setActionModel(model);
      this.d(model);
      this.b(model);
      this.f(model);
      BpmContext.removeActionModel();
      this.e(model);
   }

   protected abstract void b(T var1);

   protected abstract void c(T var1);

   protected void d(T data) {
      this.k(data);
      this.LOG.info("流程启动处理业务数据...");
      this.aC.o(data);
      this.h(data);
   }

   protected void e(T model) {
      this.j(model);
      if (this.getActionType() != ActionType.DRAFT) {
         if (model.isSource()) {
            BpmContext.cleanTread();
         }

      }
   }

   private void f(T model) {
      if (this.getActionType() == ActionType.AGREE || this.getActionType() == ActionType.OPPOSE || this.getActionType() == ActionType.START) {
         ActionCmd actionModel = BpmContext.getActionModel();
         if (actionModel != null && !(actionModel instanceof InstanceActionCmd)) {
            DefualtTaskActionCmd taskModel = (DefualtTaskActionCmd)BpmContext.getActionModel();
            if (taskModel.c() != TaskSkipType.NO_SKIP) {
               DefualtTaskActionCmd complateModel = new DefualtTaskActionCmd();
               complateModel.setBpmInstance(taskModel.getBpmInstance());
               complateModel.setBpmDefinition(taskModel.getBpmDefinition());
               complateModel.setBizDataMap(taskModel.getBizDataMap());
               complateModel.setBpmIdentities(taskModel.getBpmIdentities());
               complateModel.setBusinessKey(taskModel.getBusinessKey());
               complateModel.setActionName(ActionType.AGREE.getKey());
               complateModel.setBpmTask(taskModel.getBpmTask());
               complateModel.setOpinion(taskModel.c().getValue());
               complateModel.b();
            }
         }
      }
   }

   public void g(T model) {
      BpmContext.setActionModel(model);
      this.h(model);
      this.b(model);
      this.f(model);
      BpmContext.removeActionModel();
      this.e(model);
   }

   protected abstract void h(T var1);

   protected abstract void i(T var1);

   protected void j(T model) {
      this.i(model);
   }

   protected void k(T actionModel) {
      BpmInstance instance = (BpmInstance)actionModel.getBpmInstance();
      if (StringUtil.isEmpty(actionModel.getBusinessKey()) && StringUtil.isNotEmpty(instance.getBizKey())) {
         actionModel.setBusinessKey(instance.getBizKey());
      }

      String handler = this.n(actionModel);
      if (StringUtil.isNotEmpty(handler)) {
         try {
            HandlerUtil.a(actionModel, handler);
            this.LOG.info("执行URL表单处理器：{}", handler);
         } catch (Exception var5) {
            throw new WorkFlowException(BpmStatusCode.HANDLER_ERROR, var5);
         }
      }

      if (StringUtil.isNotEmpty(actionModel.getBusinessKey()) && StringUtil.isEmpty(instance.getBizKey())) {
         instance.setBizKey(actionModel.getBusinessKey());
         instance.setHasUpdate(true);
      }

   }

   protected void l(BaseActionCmd actionModel) {
         IBpmInstance instance = actionModel.getBpmInstance();
      if (instance.getIsForbidden() == 1) {
         throw new BusinessMessage("流程实例已经被禁止，请联系管理员", BpmStatusCode.DEF_FORBIDDEN);
      } else {
         DefaultBpmProcessDef def = (DefaultBpmProcessDef)this.a.getBpmProcessDef(instance.getDefId());
         if ("forbidden".equals(def.getExtProperties().getStatus())) {
            throw new BusinessMessage("流程定义已经被禁用，请联系管理员", BpmStatusCode.DEF_FORBIDDEN);
         } else {
            IUser user = ContextUtil.getCurrentUser();
            if (!ContextUtil.isAdmin(user)) {
               String taskId = null;
               String instId = actionModel.getInstanceId();
               if (actionModel instanceof DefualtTaskActionCmd) {
                  IBpmTask task = ((DefualtTaskActionCmd)actionModel).getBpmTask();
                  if (!user.getUserId().equals(task.getAssigneeId())) {
                     taskId = task.getId();
                     instId = null;
                     Boolean var8 = this.p.checkUserOperatorPermission(user.getUserId(), instId, taskId);
                     if (!var8) {
                        throw new BusinessException("没有该任务的操作权限", BpmStatusCode.NO_PERMISSION);
                     }
                  }
               } else if (!StringUtil.isNotEmpty(def.getProcessDefinitionId())) {
                  ;
               }
            }
         }
      }
   }

   protected void m(T actionModel) {
      IBpmInstance instance = actionModel.getBpmInstance();
      JSONObject busData = actionModel.getBusData();
      if (busData != null && !busData.isEmpty()) {
         DefaultBpmProcessDef bpmProcessDef = (DefaultBpmProcessDef)this.a.getBpmProcessDef(instance.getDefId());
         Iterator var5 = bpmProcessDef.getDataModelList().iterator();

         while(var5.hasNext()) {
            BpmDataModel dataModel = (BpmDataModel)var5.next();
            String modelCode = dataModel.getCode();
            if (busData.containsKey(modelCode)) {
               IBusinessData businessData = this.aD.parseBusinessData(busData.getJSONObject(modelCode), modelCode);
               actionModel.getBizDataMap().put(modelCode, businessData);
            }
         }

      }
   }

   protected void a(BaseActionCmd cmd, BpmNodeDef nodeDef) {
      String nodeId = nodeDef.getNodeId();
      DefaultBpmProcessDef def = (DefaultBpmProcessDef)this.a.getBpmProcessDef(cmd.getBpmInstance().getDefId());
      List<NodeInit> nodeInitList = def.d(nodeId);
      Map<String, IBusinessData> bos = cmd.getBizDataMap();
      if (!MapUtil.isEmpty(bos) && !CollectionUtil.isEmpty(nodeInitList)) {
         Map<String, Object> param = new HashMap();
         param.putAll(bos);
         param.put("bpmInstance", cmd.getBpmInstance());
         param.put("actionCmd", cmd);
         ActionType actionType = cmd.getActionType();
         param.put("submitActionDesc", actionType.getName());
         param.put("submitActionName", actionType.getKey());
         param.put("submitOpinion", cmd.getOpinion());
         param.put("isTask", false);
         if (cmd instanceof DefualtTaskActionCmd) {
            param.put("isTask", true);
            param.put("bpmTask", ((DefualtTaskActionCmd)cmd).getBpmTask());
         }

         Iterator var9 = nodeInitList.iterator();

         while(var9.hasNext()) {
            NodeInit init = (NodeInit)var9.next();
            if (StringUtil.isNotEmpty(init.getWhenSave())) {
               try {
                  this.aE.execute(init.getWhenSave(), param);
               } catch (Exception var12) {
                  throw new BusinessError(var12.getMessage(), BpmStatusCode.FLOW_DATA_EXECUTE_SHOWSCRIPT_ERROR, var12);
               }

               this.LOG.debug("执行节点数据初始化脚本{}", init.getBeforeShow());
            }
         }

      }
   }

   private String n(T actionModel) {
      String defId = actionModel.getDefId();
      BpmForm form;
      if (actionModel instanceof TaskActionCmd) {
         TaskActionCmd cmd = (TaskActionCmd)actionModel;
         String nodeId = cmd.getNodeId();
         BpmNodeDef nodeDef = this.a.getBpmNodeDef(defId, nodeId);
         form = nodeDef.getForm();
      } else {
         BpmNodeDef nodeDef = this.a.getStartEvent(defId);
         form = nodeDef.getForm();
      }

      if (form == null || form.isFormEmpty()) {
         DefaultBpmProcessDef def = (DefaultBpmProcessDef)this.a.getBpmProcessDef(defId);
         form = def.getGlobalForm();
      }

      return form != null ? form.getFormHandler() : null;
   }

   public Boolean isDefault() {
      return true;
   }

   public String getDefaultBeforeScript() {
      return "";
   }
}
